package com.mactivity.database;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.mactivity.helper.Constants;

/**
 * This class contains the method to check if the email Id is registered with
 * MACtivity or not.
 * 
 * @author Shwetha Narayan
 * 
 */
public class ForgotPasswordFunctions {

	/**
	 * This method validates that the email Id enetered by the user is
	 * registered with MACtivity.
	 * 
	 * @param emailId
	 *            String email entered by the user in order to reset the
	 *            password.
	 * @return String "0" if the email Id is registered and "1" otherwise.
	 */
	public String validateEmailId(String emailId) {
		String result = null;

		try {
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("emailID", emailId));
			String response = null;

			Log.d(Constants.LOG_DATABASE_TAG, "Calling  database");

			// Check if the
			response = CustomHttpClient
					.executeHttpPost(
							Constants.DATABASE_URL+"Registration/reg_emailchk_script.php",
							postParameters);

			Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved");

			response = response.replaceAll("\n", "");

			if (!response.toString().equalsIgnoreCase("null")) {
				Log.d(Constants.LOG_DATABASE_TAG, "Response from database: "
						+ response);
				result = response;
			} else {
				Log.d(Constants.LOG_DATABASE_TAG,
						"Invalid User name password combination");
			}

		} catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,
					"Error in http connection while connecting to the database!!"
							+ e.getMessage());
		}

		return result;
	}

	/**
	 * This method will update the user details with the password that is reset.
	 * 
	 * @param emailId
	 *            String email Id entered by the user.
	 * @param password
	 *            String randomly generated password that will be updated for
	 *            the given email Id.
	 * @return
	 */
	public boolean resetPassword(String emailId, String password) {
		
		boolean passwordUpdated = false;
		
		try{
			
			if(!emailId.equals(null) && !password.equals(null)){
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("emailId", emailId));
				postParameters.add(new BasicNameValuePair("password", password));
				String response = null;

				Log.d(Constants.LOG_DATABASE_TAG, "Calling  database");

				// Check if the
				response = CustomHttpClient
						.executeHttpPost(
								Constants.DATABASE_URL+"UpdatePassword/password_update_script.php",
								postParameters);

				Log.d(Constants.LOG_DATABASE_TAG, "Database response recieved");
				
				System.out.println(response);
				passwordUpdated = true;		
				
			}else{
				return passwordUpdated; 
			}
			
		}catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,
					"Error in http connection while connecting to the database!!"
							+ e.getMessage());
		}

		return passwordUpdated;
	}

}
